﻿namespace EOM.TSHotelManagement.Shared
{
    public static class DiscountConverter
    {
        /// <summary>
        /// 最大允许的折扣百分比值（默认100%）
        /// </summary>
        public const decimal MAX_DISCOUNT_PERCENT = 100m;

        /// <summary>
        /// 最小允许的折扣百分比值
        /// </summary>
        public const decimal MIN_DISCOUNT_PERCENT = 0m;

        /// <summary>
        /// 将百分比折扣值转换为中文xx折表示
        /// 如果值无效，返回"无效折扣"
        /// </summary>
        public static string ToZheString(this decimal discountPercent)
        {
            if (discountPercent < MIN_DISCOUNT_PERCENT || discountPercent > MAX_DISCOUNT_PERCENT)
            {
                return "无效折扣";
            }

            if (discountPercent == 100)
            {
                return "无折扣";
            }

            if (discountPercent < 10)
            {
                return $"零点{discountPercent:#.0}折";
            }

            return discountPercent switch
            {
                var x when x % 10 == 0 => $"{ConvertDigit((int)x / 10)}折({discountPercent}%)",
                _ => $"{ConvertDigit((int)discountPercent / 10)}{ConvertDigit((int)discountPercent % 10)}折({discountPercent}%)"
            };
        }

        /// <summary>
        /// 数字转中文大写（0-9）
        /// </summary>
        private static string ConvertDigit(int num) => num switch
        {
            0 => "〇",
            1 => "一",
            2 => "二",
            3 => "三",
            4 => "四",
            5 => "五",
            6 => "六",
            7 => "七",
            8 => "八",
            9 => "九",
            _ => throw new ArgumentOutOfRangeException(nameof(num), "仅支持0-9数字转换")
        };

        public static decimal RealAmount(decimal discountPercent, decimal itemAmount)
        {
            decimal discountedAmount;

            if (discountPercent == 0)
            {
                discountedAmount = 0;
            }
            else if (discountPercent == 100)
            {
                discountedAmount = itemAmount;
            }
            else
            {
                decimal discountRate = discountPercent / 100M;
                discountedAmount = itemAmount * discountRate;
            }

            return discountedAmount;
        }
    }
}
